//
//  PasswordViewController.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/2/26.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa


class PasswordViewController: ViewController {

    
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var password2TextField: UITextField!
    @IBOutlet weak var enterBtn: UIButton!
    
    var verifyCode: String!
    var mobile: String!
    var sms: String!
    
    var type: LoginType = .rigist
    
    static func controller(mobile:String, verifyCode: String, sms:String, type: LoginType = .rigist) -> PasswordViewController {
        let vc = PasswordViewController.viewController(storyboardName: "Account") as! PasswordViewController
        vc.verifyCode = verifyCode
        vc.sms = sms
        vc.mobile = mobile
        vc.type = type
        return vc
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        if type == .rigist {
            self.navigationItem.title = "设置登录密码"
        }
        else {
            self.navigationItem.title = "重设登录密码"
        }
        
        let passwordObserable: Observable<Bool> = passwordTextField.rx.text.orEmpty.map { text in
            var result = false
            result = (text.count >= 6 && text.count <= 20)
            return result
            }
        
        passwordObserable.subscribe(onNext: { (valid) in
            if valid   {
                self.enterBtn.isEnabled = true
                self.enterBtn.backgroundColor = UIColor.lsf_color16(0xF56E77)
            }else{
                self.enterBtn.isEnabled = false
                self.enterBtn.backgroundColor = UIColor.lightGray
            }
        }).disposed(by: disposeBag)
        
        enterBtn.rx.tap.bind {
            let psd = self.passwordTextField.text ?? ""
            let psd2 = self.password2TextField.text ?? ""
            if psd != psd2 {
                lsf_alert("两次输入的密码不一致", message: "请重新输入", viewController: self)
                return
            }
            
            startAnimating()
            
            if self.type == .rigist {
                AccountService.setPassword(mobile: self.mobile, verifyCode: self.verifyCode, sms: self.sms, password: psd, success: { [weak self] (_, _) in
                    stopAnimating()
                    guard let strongSelf = self else { return }
                    //注册成功，跳转到考试等级选择页面
                    let levelVC = ExamLevelViewController.controller(.set)
                    strongSelf.navigationController?.pushViewController(levelVC, animated: true)
                    
                    }, failure: { [weak self] (code, msg) in
                        stopAnimating()
                        lsf_alert(msg, viewController:self)
                })
            }
            else if self.type == .changePsd {
                AccountService.changePassword(mobile: self.mobile, verifyCode: self.verifyCode, sms: self.sms, password: psd, success: { [weak self] in
                    stopAnimating()
                    // 发送通知：
                    NotificationCenter.default.post(name: Notification.Name(rawValue: LoginViewControllerNotification), object: self?.type)
                    
                }, failure: { [weak self] (code, msg) in
                    stopAnimating()
                    lsf_alert(msg, viewController:self)
                })
            }
        }.disposed(by: disposeBag)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
